﻿// -----------------------------------------------------------------------
// <copyright file="CharacterDemon.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace BloodThirsty
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CharacterDemon
    {
        private Texture2D[] _DemonMove;

        public Texture2D[] DemonMove
        {
            get { return _DemonMove; }
            set { _DemonMove = value;
            nTexture = _DemonMove.Length;
            iTexture = 0;


            
            }
        }

        private Texture2D[] _DemonStand;

        public Texture2D[] DemonStand
        {
            get { return _DemonStand; }
            set { _DemonStand = value;
            nTexture = _DemonStand.Length;
            iTexture = 0;

            }
        }

        private Texture2D[] _DemonAttack;

        public Texture2D[] DemonAttack
        {
            get { return _DemonAttack; }
            set { _DemonAttack = value;
            nTexture = _DemonAttack.Length;
            iTexture = 0;

            
            }
        }
        private Texture2D[] _DemonDying;

        public Texture2D[] DemonDying
        {
            get { return _DemonDying; }
            set { _DemonDying = value;
            nTexture = _DemonDying.Length;
            iTexture = 0;

            
            }
        }
        private Texture2D[] _DemonDyed;

        public Texture2D[] DemonDyed
        {
            get { return _DemonDyed; }
            set { _DemonDyed = value;
            nTexture = _DemonDyed.Length;
            iTexture = 0;

            
            }
        }

        private double _Left;

        public double Left
        {
            get { return _Left; }
            set { _Left = value; }
        }

        private double _Top;

        public double Top
        {
            get { return _Top; }
            set { _Top = value; }
        }

        private int _nTexture;

        public int nTexture
        {
            get { return _nTexture; }
            set { _nTexture = value; }
        }

        private int _iTexture;

        public int iTexture
        {
            get { return _iTexture; }
            set { _iTexture = value; }
        }

        public CharacterDemon(Texture2D[] Stand, Texture2D[] Move, double left, double top)
        {
            DemonStand = Stand;
            DemonMove = Move;
          
            Left = left;
            Top = top;
        }
        private int _hChuyenDong;

        public int HChuyenDong
        {
            get { return _hChuyenDong; }
            set { _hChuyenDong = value; }
        }

        public double GioiHanBenPhai(double X)
        {
            double GiaTriTung;
            X = X - GiuaManHinhX;
            GiaTriTung = X / 4;
            GiaTriTung = GiuaManHinhY -GiaTriTung;
            return GiaTriTung;
            // Ham nay dung de lay ra gia tri gioi han theo phuong trinh y = 1/4x (phuong trinh nhom tu quy uoc)
            // Neu Y cua chuot lon hon y trong phuong trinh thi chung to no nam ngoai vung
        }

        public double GioiHanBenTren(double X)
        {
            double GiaTriTung;
            GiaTriTung = (X*3)/2;
            return GiaTriTung;
            // Ham nay dung de lay ra gia tri gioi han theo phuong trinh y = x (phuong trinh nhom tu quy uoc)
            // Neu Y cua chuot lon hon y trong phuong trinh thi chung to no nam ngoai vung
        }

        
        // Do ko the lay dc vi tri chinh giua man hinh nen set cung vao toa do nay
        int GiuaManHinhX = 685; // Day La vi tri nhan vat chinh
        int GiuaManHinhY = 376; // Day la vi tri nhan vat chinh
        

        public int HuongChuyenDong(double X, double Y)
        {
            // Ham huong chuyen dong dung de xem chuot nam o vi tri nao so voi diem chinh giua cua man hinh
            // Sau khi xem xet vi tr nay dung de ve cac sprite 1 cach thich hop nhat
            int Huong = 0 ;
            if (X > GiuaManHinhX) // Dieu nay co nghia la chuot dang nam ben phai cua man hinh
            {
                if (Y < GiuaManHinhY) // dieu nay co nghia la chuot dang nam o nua tren man hinh
                {
                    if (Y > GioiHanBenPhai(X))// Dieu nay co nghia la chuot dang nam trong vung khien cho sprite di ve phia ben phai
                    {
                        Huong = 1;// Quy uoc Sprite di ve phia ben phai
                        
                       
                    }
                    else  // Dieu nay co nghia la chuot dang nam o vung khien cho sprite di len phia tren
                    {
                        if((GiuaManHinhY - Y) > (GioiHanBenTren(X -GiuaManHinhX)))
                        {
                            Huong = 2; // quy uoc sprite di len ben tren
                        }
                        else
                        {
                            Huong = 3;// Chuot di ve nua tren va ben phai

                        }
                    }
                    

                }
                
                else
                {
                    // Diau nay co nghia la chuot dang nam o ben nua duoi cua man hinh
                    Y = Y - (2 * (Y - GiuaManHinhY));// Lam cho Y doi xung giong nhu dang o ben nua tren man hinh
                    // Lam vay de co the de dang ap dung cac ham nhu o tren vay

                    if (Y > GioiHanBenPhai(X))// Dieu nay co nghia la chuot dang nam trong vung khien cho sprite di ve phia ben phai
                    {
                        Huong = 1;// Quy uoc Sprite di ve phia ben phai
                        
                    }
                    else  // Dieu nay co nghia la chuot dang nam o vung khien cho sprite dixuong phia duoi
                    {
                        if((GiuaManHinhY - Y) > (GioiHanBenTren(X - GiuaManHinhX))) 
                        {
                            Huong = 4; // quy uoc sprite di xuong duoi
                        }
                        else
                        {
                            Huong = 5;// Chuot di ve nua duoi va ben phai

                        }
                    }
                    
                }
            }
            else // Dieu nay co nghi la chuot dang nam ben trai cua man hinh
            {
                X = X + (2 * (GiuaManHinhX - X)); // Lam buoc nay de lam chuot doi xung qua ben phai cua man hinh
                // Chung ta tiep tuc su dung lai cac ham giong nhu la chuot dang nam ben phai man hinh
                // Chi la xuat ra huong nguoc lai, doi xung qua
                if (Y < GiuaManHinhY) // dieu nay co nghia la chuot dang nam o nua tren man hinh
                {
                    if (Y > GioiHanBenPhai(X))// Dieu nay co nghia la chuot dang nam trong vung khien cho sprite di ve phia ben trai
                    {
                        Huong = 6;// Quy uoc Sprite di ve phia ben trai
                       
                    }
                    else  // Dieu nay co nghia la chuot dang nam o vung khien cho sprite di len phia tren
                    {
                        if((GiuaManHinhY - Y) > (GioiHanBenTren(X - GiuaManHinhX)))
                        {
                            Huong = 2; // quy uoc sprite di len ben tren
                        }
                        else
                        {
                            Huong = 7;// Chuot di ve nua tren va ben trai

                        }
                    }
                    

                }
                else
                {
                    // Diau nay co nghia la chuot dang nam o ben nua duoi cua man hinh
                    Y = Y - (2 * (Y - GiuaManHinhY));// Lam cho Y doi xung giong nhu dang o ben nua tren man hinh
                   
                    // Lam vay de co the de dang ap dung cac ham nhu o tren vay

                    if (Y > GioiHanBenPhai(X))// Dieu nay co nghia la chuot dang nam trong vung khien cho sprite di ve phia ben trai
                    {
                        Huong = 6;// Quy uoc Sprite di ve phia ben trai
                        
                    }
                    else  // Dieu nay co nghia la chuot dang nam o vung khien cho sprite dixuong phia duoi
                    {
                        if((GiuaManHinhY - Y) > (GioiHanBenTren(X - GiuaManHinhX)))
                        {
                            Huong = 4; // quy uoc sprite di xuong duoi
                        }
                        else
                        {
                            Huong = 8;// Chuot di ve nua duoi va ben trai

                        }
                    }
                    
                }
            }
            return Huong;

        }
        int BienTam = 0;
        // Ham nay dung de xem co dc phap cap nhat lai nTexture va iTexture. No se cho phep cap nhat nhat khi chuot di qua 1 vung khac voi vung luc dau
        // Noi thang ra la HchuyenDong bi Thay doi.
        public bool DieuKienCapNhat()
        {
            if (BienTam == HChuyenDong)
            {
                return false;
            }
            else
            {
                BienTam = HChuyenDong;
                return true;
            }
        }
        

        // Ham nay chuyen dung de cap nhat nTexture va iTexture
        // Cap nhat cai nay la vi demon chua 1 chuoi rat nhieu texture voi cai hanh dong khac nhau
        // Voi moi vi tri chuot ta chi can 1 so texture nhat dinh
        public void CapNhatDuLieu(int ChuyenDong)
        {
            if (ChuyenDong == 1)// Di ve ben phai
            {
                iTexture = 0;
                nTexture = 8;
            }
            if (ChuyenDong == 3)// Di len nua tren ben phai
            {
                iTexture = 8;
                nTexture = 16;
            }
            if (ChuyenDong == 2) //Di len tren
            {
                iTexture = 16;
                nTexture = 24;
            }
            if (ChuyenDong == 7) //Di len nua tren ben trai
            {
                iTexture = 24;
                nTexture = 32;
            }
            if (ChuyenDong == 6) // Di ve ban trai
            {
                iTexture = 32;
                nTexture = 40;
            }
            if (ChuyenDong == 8) //Di ve nua duoi ben trai
            {
                iTexture = 40;
                nTexture = 48;
            }
            if (ChuyenDong == 4) //Di xuong duoi
            {
                iTexture = 48;
                nTexture = 56;
            }
            if (ChuyenDong == 5) //Di ve nua duoi ben phai
            {
                iTexture = 56;
                nTexture = 64;
            }

        }

        public void Update(GameTime gameTime)
        {
            // Voi moi huong khac nhau ta lua sprite de ve khac nhau

            if (DieuKienCapNhat() == true)
            {
                CapNhatDuLieu(HChuyenDong);

            }
            else
            {

                
                iTexture = (iTexture + 1) % nTexture;
                // Co cai nay la vi doi luc itexture tro ve 0 va ve nhung texture tako mong muon
                // If ben duoi dung de tranh nhung truong hop nhu vay
                if (iTexture == 0) 
                {
                    CapNhatDuLieu(HChuyenDong);
                }
                
            }

        }

        public void Draw(GameTime gametime, SpriteBatch spriteBatch)
        {
                     
            spriteBatch.Draw(DemonMove[iTexture], new Vector2((float)Left, (float)Top), Color.White);
        }
    }
}
